package com.wanxi.service.impl;

import com.wanxi.dao.NewsDao;
import com.wanxi.dao.NewsclassificationDao;
import com.wanxi.model.NewsModel;
import com.wanxi.model.NewsclassificationModel;
import com.wanxi.model.ResultModel;
import com.wanxi.service.NewsService;
import com.wanxi.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @author 杨金乾
 */
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsDao newsDao;
    @Autowired
    NewsclassificationDao newsclassificationDao;
    @Override
    public ResultModel enable(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.enable(newsModel));
    }

    @Override
    public ResultModel add(NewsModel newsModel) {
        SnowflakeIdWorker snowflakeIdWorker=new SnowflakeIdWorker(WorkId.WORK_ID);
        newsModel.setId(snowflakeIdWorker.nextId());
        return ResultModel.getResult(newsDao.add(newsModel));
    }

    @Override
    public ResultModel update(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.update(newsModel));
    }

    @Override
    public ResultModel delete(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.delete(newsModel));
    }

    @Override
    public ResultModel findById(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.findById(newsModel));
    }

    @Override
    public ResultModel findAll(NewsModel newsModel) {
        newsModel.setLimit(10);
        newsModel.setPage((newsModel.getPage()-1)*10);
        List<NewsModel> newsModelList = newsDao.findAll(newsModel);
        for (NewsModel model : newsModelList) {
            Long newsclassificationId = model.getNewsclassificationId();
            NewsclassificationModel newsclassificationModel = new NewsclassificationModel();
            newsclassificationModel.setId(Long.valueOf(newsclassificationId));
            newsclassificationModel = newsclassificationDao.findById(newsclassificationModel);
            model.setNewsclassificationName(newsclassificationModel.getName());
        }

        return ResultModel.getResult(newsModelList, newsDao.count(newsModel));
    }

    @Override
    public ResultModel count(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.count(newsModel));
    }

    @Override
    public ResultModel isShow(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.isShow(newsModel));
    }

    @Override
    public ResultModel isRecommend(NewsModel newsModel) {
        return ResultModel.getResult(newsDao.isRecommend(newsModel));
    }

    @Override
    public ResultModel whereShow(NewsModel newsModel) {
        return null;
    }
}
